function getUrlVars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}

var notes = unescape(getUrlVars()["notes"]);

$(document).ready(function() {
	$("#description").html(notes);

	$.ajax({
		url: "http://hiveminder.com/=/action/AboutMe.json",
		type: "POST",
		success: function(data, textStatus, jqXHR) {
			if(data.content.current_user.name != undefined) {
				//$("#usersName").html(data.content.current_user.name);
				$("#authYes").show();
				$("#authNo").hide();
				$("#summary").focus();
			} else {
				$("#authYes").hide();
				$("#authNo").show();
				$("#address").focus();
			}
		},
		error: function(jqXHR, textStatus, errorThrown) {
			$("#wrapper").append("<div id='misc'>");
			$("#misc").addClass('fail').html(textStatus);
		},
		dataType: "json"
	});
	
	$("#hmbd-submit").bind("click", function() {
		$("#hmbd-status").removeClass("success").removeClass("fail").html("");
		$("#hmbd-status").show().html("<img src='images/ajax-loader.gif' />");
		var formData = {	summary:		$("#summary").val(),
							description:	$("#description").val(),
							priority:		$("#priority").val(),
							due:			$("#due").val(),
							tags:			$("#tags").val()};

		$.ajax({
			url: "http://hiveminder.com/=/action/CreateTask.json",
			type: "POST",
			data: formData,
			success: function(data, textStatus, jqXHR) {
				$("#hmbd-status").addClass('success').html("Success!").delay(3000).fadeOut(1000);
				setTimeout('window.close();', 1000);
			},
			error: function(jqXHR, textStatus, errorThrown) {
				$("#hmbd-status").addClass('fail').html(textStatus);
			},
			dataType: "json"
		});
	});
	
	$("#login_submit").bind("click", function() {
		var remYN;
		if($("#remember").is(":checked")) {
			remYN = 1;
		} else {
			remYN = 0;
		}
		
		var formData = {	address:	$("#address").val(),
							password:	$("#password").val(),
							remember:	remYN};
		
		$.ajax({
			url: "http://hiveminder.com/=/action/Login.json",
			type: "POST",
			data: formData,
			success: function(data, textStatus, jqXHR) {
				if(data.failure==1) {
					$("#loginStatus").html(data.error);
				} else {
					$("#loginStatus").html(data.message);
					setTimeout("window.location.reload()", 1000);
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				$("#loginStatus").html("There was an error processing your request.");
			},
			dataType: "json"
		});
	});
});